<?php

require_once 'vendor/autoload.php';

// 加载Laravel应用
$app = require_once 'bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

echo "=== 调试日志统计API ===\n";

try {
    // 模拟用户登录
    $user = \App\Models\User::find(1);
    echo "测试用户: {$user->username} (ID: {$user->id})\n";

    // 创建模拟请求
    $request = new \Illuminate\Http\Request();
    $request->setMethod('GET');
    $request->server->set('REQUEST_URI', '/api/logs/statistics');
    $request->server->set('REQUEST_METHOD', 'GET');

    // 设置用户认证
    \Illuminate\Support\Facades\Auth::setUser($user);
    echo "用户认证设置完成\n";

    // 测试权限服务
    $permissionService = app(\App\Services\PermissionService::class);

    $hasPermission = $permissionService->hasPermission($user, 'system.logs');
    echo "用户是否有system.logs权限: " . ($hasPermission ? '是' : '否') . "\n";

    $canAccessRoute = $permissionService->canAccessRoute($user, 'GET', '/api/logs/statistics');
    echo "用户能否访问路由: " . ($canAccessRoute ? '是' : '否') . "\n";

    // 测试日志服务
    $logService = app(\App\Services\LogService::class);
    echo "日志服务创建成功\n";

    $stats = $logService->getStatistics([]);
    echo "日志统计获取成功\n";
    echo "统计数据: " . json_encode($stats, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";

} catch (\Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
    echo "文件: " . $e->getFile() . " 行: " . $e->getLine() . "\n";
    echo "堆栈:\n" . $e->getTraceAsString() . "\n";
}
